"""Main Controller"""
from gigboard.lib.base import BaseController
from tg import expose, flash
from pylons.i18n import ugettext as _
#from tg import redirect, validate
from gigboard.model import DBSession, metadata
#from dbsprockets.dbmechanic.frameworks.tg2 import DBMechanic
#from dbsprockets.saprovider import SAProvider
from tg.ext.repoze.who import authorize
#from gigboard.controllers.secc import Secc
from gigboard import model
from datetime import datetime
import simplejson
import pycountry

class RootController(BaseController):
    #admin = DBMechanic(SAProvider(metadata), '/admin')
    #secc = Secc()

    @expose('gigboard.templates.index')
    def index(self, *args, **kw):
        active = model.Gig.query.filter(model.Gig.is_active==True)
        active = active.filter(model.Gig.expiry_date >= datetime.utcnow()).all()
        return dict(active=active)
        
    @expose('gigboard.templates.add_gig')
    def add(self, *args, **kw):
        return dict(countries=pycountry.countries)
        
    @expose(content_type='application/json')
    def add_endpoint(self, *args, **kw):
        return dict()

    @expose(content_type='application/json')
    def gig_filter(self, *args, **kw):
        return simplejson.dumps(dict(error="not implemented"))
        
    @expose('gigboard.templates.about')
    def about(self):
        return dict(page='about')

    @expose('gigboard.templates.login')
    def login(self, **kw):
        came_from = kw.get('came_from', '/')
        return dict(page='login', header=lambda *arg: None,
                    footer=lambda *arg: None, came_from=came_from)
